﻿Imports System.Windows.Forms

Public Class fSetPlatbyDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        '------------updating dataset/database table "seznam_vstupy" after adding a new row to the table
        If Me.TextBox1.Text <> "" Then
            Try
                If Me.DtbDataSet1.seznam_PrijemTypPlatby.Count() > 0 Then
                    _lastID = Me.DtbDataSet1.seznam_PrijemTypPlatby.Rows.Item(Me.DtbDataSet1.seznam_PrijemTypPlatby.Count() - 1).Item(0) 'gets id of last row
                ElseIf Me.DtbDataSet1.seznam_PrijemTypPlatby.Count() = 0 Then
                    _lastID = 0
                End If
                Dim row As DataRow = Me.DtbDataSet1.seznam_PrijemTypPlatby.NewRow() 'sets new datarow
                row(0) = _lastID + 1 'generates new id value incrementally
                row(1) = Me.TextBox1.Text 'assigns textbox input to new row/column
                Me.DtbDataSet1.seznam_PrijemTypPlatby.Rows.Add(row) 'adds new row to the table (collection)
                'Me.DtbDataSet1.seznam_PrijemTypPlatby.AcceptChanges()
                Me.Seznam_TypPlatbyTableAdapter1.Update(DtbDataSet1.seznam_PrijemTypPlatby) 'updates table in database
                'confirms changes, new values are now original ones
            Catch except As Exception
                MsgBox(except.Message, MsgBoxStyle.Critical, "Error when saving data")
            End Try
        End If
        '-----------end of update
        '----------------reloading data from seznam_vstupy in "fSetVstupy" form
        Dim fill As String = ""
        Try
            For i As Int16 = 0 To Me.DtbDataSet1.seznam_PrijemTypPlatby.Rows.Count() - 1
                fill = fill & Convert.ToString(i + 1) & ". " & Me.DtbDataSet1.seznam_PrijemTypPlatby.Rows.Item(i).Item(1) & vbCrLf
            Next i
            My.Forms.fSetPlatby.tboxSeznamTypuPlateb.Text = fill
            fill = ""
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error when loading data")
        End Try
        '----------------end of loading data
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub fSetPlatbyDialog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.Seznam_TypPlatbyTableAdapter1.Fill(Me.DtbDataSet1.seznam_PrijemTypPlatby)
    End Sub
End Class
